/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package simplemetrosystem.view.table;

import java.awt.Font;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Observable;
import java.util.Observer;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import simplemetrosystem.control.Calculation.CalculManager;
import simplemetrosystem.control.ElementManager;
import simplemetrosystem.model.Element.MetroLine;
import simplemetrosystem.model.Element.MetroStation;
import simplemetrosystem.model.charlanguage.NameMap;
import simplemetrosystem.view.Layout.GBC;
import simplemetrosystem.view.MetroFrame;
import simplemetrosystem.view.action.CloseTabActionListener;

/**
 *
 * @author Eric Qian
 */
public class CalculPane extends JPanel implements Observer {

    public CalculPane(MetroFrame frame) {
        this.frame = frame;
        init();
    }

    /**
     * 
     */
    private void init() {
        nameMap = NameMap.getInstance();
        calManager = CalculManager.getInstance();
        changeGroup = new ButtonGroup();
        change = false;
        lineA = ElementManager.getInstance().getLineAt(0);
        lineB = ElementManager.getInstance().getLineAt(0);
        stationA = ElementManager.getInstance().getLineAt(0).getStationAt(0);
        stationB = ElementManager.getInstance().getLineAt(0).getStationAt(0);
        calManager.setStationA(stationA);
        calManager.setStationB(stationB);
        //this.setBackground(Color.PINK);
        this.setLayout(new GridBagLayout());

        add(getModelLabel(), new GBC(0, 0, 3, 1).setFill(GBC.HORIZONTAL).setWeight(1, 0).setInset(5));
        add(getModelBox(), new GBC(3, 0, 3, 1).setFill(GBC.HORIZONTAL).setWeight(1, 0).setInset(5));
        add(getStALabel(), new GBC(0, 1, 2, 1).setFill(GBC.HORIZONTAL).setWeight(1, 0).setInset(5));
        add(getStALineBox(), new GBC(2, 1, 2, 1).setFill(GBC.HORIZONTAL).setWeight(1, 0).setInset(5));
        add(getStAStationBox(), new GBC(4, 1, 2, 1).setFill(GBC.HORIZONTAL).setWeight(1, 0).setInset(5));
        add(getStBLabel(), new GBC(0, 2, 2, 1).setFill(GBC.HORIZONTAL).setWeight(1, 0).setInset(5));
        add(getStBLineBox(), new GBC(2, 2, 2, 1).setFill(GBC.HORIZONTAL).setWeight(1, 0).setInset(5));
        add(getStBStationBox(), new GBC(4, 2, 2, 1).setFill(GBC.HORIZONTAL).setWeight(1, 0).setInset(5));
        add(getChangeLabel(), new GBC(0, 3, 2, 1).setFill(GBC.HORIZONTAL).setWeight(1, 0).setInset(5));
        add(getNoButton(), new GBC(2, 3, 2, 1).setWeight(1, 0).setInset(5));
        add(getYesButton(), new GBC(4, 3, 2, 1).setWeight(1, 0).setInset(5));
        add(new JScrollPane(getDisplay()), new GBC(0, 4, 6, 1).setFill(GBC.BOTH).setWeight(1, 1).setInsets(5, 0, 5, 0));
        add(getCalButton(), new GBC(0, 5, 4, 1).setFill(GBC.HORIZONTAL).setWeight(1, 0).setInset(5));
        add(getCloseButton(), new GBC(4, 5, 2, 1).setFill(GBC.HORIZONTAL).setWeight(1, 0).setInset(5));
    }

    public JLabel getModelLabel() {
        if (modelLabel == null) {
            modelLabel = new JLabel();
            modelLabel.setText(nameMap.getName("Model"));
            modelLabel.setFont(new Font(null, Font.BOLD, 20));
        }
        return modelLabel;
    }

    public JComboBox getModelBox() {
        if (modelBox == null) {
            modelBox = new JComboBox();
            calManager = CalculManager.getInstance();
            for (int i = 0; i < calManager.getFindSize(); i++) {
                modelBox.addItem(calManager.getFindName(i));
            }
            modelBox.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {
                    calManager.setCurrentWay(modelBox.getSelectedIndex());
                    frame.getInfPane().setHighLight(null, false);
                    displayArea.setText("");
                }
            });

        }
        return modelBox;
    }

    public JLabel getStALabel() {
        if (stALabel == null) {
            stALabel = new JLabel();
            stALabel.setText(nameMap.getName("Station A"));
            stALabel.setFont(new Font(null, Font.BOLD, 20));
        }
        return stALabel;
    }

    public JComboBox getStALineBox() {
        if (stALineBox == null) {
            stALineBox = new JComboBox();
            elementManager = ElementManager.getInstance();
            for (int i = 0; i < elementManager.getLinesSize() - 2; i++) {
                stALineBox.addItem(elementManager.getLineAt(i).getDisplayName());
            }
            stALineBox.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {
                    lineA = elementManager.getLineAt(stALineBox.getSelectedIndex());
                    stAStationBox.removeAllItems();
                    for (int i = 0; i < lineA.getStationsSize(); i++) {
                        stAStationBox.addItem(lineA.getStationAt(i).getDisplayName());
                    }
                    stationA = lineA.getStationAt(0);
                    frame.getInfPane().setHighLight(null, false);
                    displayArea.setText("");
                    repaint();
                }
            });

        }
        return stALineBox;
    }

    public JComboBox getStAStationBox() {
        if (stAStationBox == null) {
            stAStationBox = new JComboBox();

            for (int i = 0; i < lineA.getStationsSize(); i++) {
                stAStationBox.addItem(lineA.getStationAt(i).getDisplayName());
            }

            stAStationBox.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {
                    if (lineA != null) {
                        if (lineA != null && lineA.getStationsSize() >
                                stAStationBox.getSelectedIndex() &&
                                stAStationBox.getSelectedIndex() > 0) {
                            stationA = lineA.getStationAt(stAStationBox.getSelectedIndex());
                        }
                    }
                    frame.getInfPane().setHighLight(null, false);
                    displayArea.setText("");
                }
            });

        }
        return stAStationBox;
    }

    public JLabel getStBLabel() {
        if (stBLabel == null) {
            stBLabel = new JLabel();
            stBLabel.setText(nameMap.getName("Station B"));
            stBLabel.setFont(new Font(null, Font.BOLD, 20));
        }
        return stBLabel;
    }

    public JComboBox getStBLineBox() {
        if (stBLineBox == null) {
            stBLineBox = new JComboBox();
            elementManager = ElementManager.getInstance();
            for (int i = 0; i < elementManager.getLinesSize() - 2; i++) {
                stBLineBox.addItem(elementManager.getLineAt(i).getDisplayName());
            }
            stBLineBox.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {
                    lineB = elementManager.getLineAt(stBLineBox.getSelectedIndex());
                    stBStationBox.removeAllItems();
                    for (int i = 0; i < lineB.getStationsSize(); i++) {
                        stBStationBox.addItem(lineB.getStationAt(i).getDisplayName());
                    }
                    stationB = lineB.getStationAt(0);
                    frame.getInfPane().setHighLight(null, false);
                    displayArea.setText("");
                    repaint();
                }
            });

        }
        return stBLineBox;
    }

    public JComboBox getStBStationBox() {
        if (stBStationBox == null) {
            stBStationBox = new JComboBox();
            for (int i = 0; i < lineB.getStationsSize(); i++) {
                stBStationBox.addItem(lineB.getStationAt(i).getDisplayName());
            }

            stBStationBox.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {
                    if (lineB != null && lineB.getStationsSize() >
                            stBStationBox.getSelectedIndex() &&
                            stBStationBox.getSelectedIndex() > 0) {
                        stationB = lineB.getStationAt(stBStationBox.getSelectedIndex());
                    }
                    frame.getInfPane().setHighLight(null, false);
                    displayArea.setText("");
                }
            });
        }
        return stBStationBox;
    }

    public JLabel getChangeLabel() {
        if (changeLabel == null) {
            changeLabel = new JLabel();
            changeLabel.setText(nameMap.getName("Transportation Card"));
            changeLabel.setFont(new Font(null, Font.BOLD, 20));
        }
        return changeLabel;
    }

    public JRadioButton getNoButton() {
        if (noButton == null) {
            noButton = new JRadioButton();
            noButton.setSelected(true);
            noButton.setText(nameMap.getName("No"));
            noButton.setFont(new Font(null, Font.BOLD, 20));
            changeGroup.add(noButton);
            noButton.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {
                    change = false;
                    frame.getInfPane().setHighLight(null, false);
                    displayArea.setText("");
                }
            });
        }
        return noButton;
    }

    public JRadioButton getYesButton() {
        if (yesButton == null) {
            yesButton = new JRadioButton();
            yesButton.setText(nameMap.getName("Yes"));
            yesButton.setFont(new Font(null, Font.BOLD, 20));
            changeGroup.add(yesButton);
            yesButton.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {
                    change = true;
                    frame.getInfPane().setHighLight(null, false);
                    displayArea.setText("");
                }
            });

        }
        return yesButton;
    }

    public JButton getCalButton() {
        if (calButton == null) {
            calButton = new JButton();
            calButton.setText(nameMap.getName("Calculation"));
            calButton.setToolTipText(nameMap.getName("Calculation"));
            calButton.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {
                    //System.out.println(stationA.getDisplayName());
                    //System.out.println(stationB.getDisplayName());
                    calManager.setStationA(stationA);
                    calManager.setStationB(stationB);
                    calManager.findWay(change);
                    displayArea.setText(calManager.getOutput());
                    frame.getInfPane().setHighLight(calManager.getCalElement(), true);
                }
            });
        }
        return calButton;
    }

    public JButton getCloseButton() {
        if (closeButton == null) {
            closeButton = new JButton();
            closeButton.setText(nameMap.getName("Close Tab"));
            closeButton.setToolTipText("Close Tab");
            closeButton.addActionListener(new CloseTabActionListener(frame.getInfPane()));
        }
        return closeButton;
    }

    public JPanel getDisplay() {
        if (display == null) {
            display = new JPanel();
            display.setLayout(new GridBagLayout());
            displayArea = new JTextArea();
            displayArea.setFont(new Font(null, Font.BOLD, 20));
            display.add(displayArea, new GBC(0, 0).setWeight(1, 1).setFill(GBC.BOTH));
        }
        return display;
    }

    public void setStationA(MetroStation stA) {
        MetroLine lA = stA.getLineAt(0);
        MetroStation sA = stA;
        //System.out.println("!" + stationA.getDisplayName());
        stALineBox.setSelectedIndex(elementManager.getIndexOfLine(lA));
        stAStationBox.setSelectedIndex(lineA.getIndexOfStation(sA));

        repaint();
    }

    public void setStationB(MetroStation stB) {
        MetroLine lB = stB.getLineAt(0);
        MetroStation sB = stB;
        stBLineBox.setSelectedIndex(elementManager.getIndexOfLine(lB));
        stBStationBox.setSelectedIndex(lineB.getIndexOfStation(sB));

        repaint();
    }

    public void update(Observable o, Object arg) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    private MetroFrame frame;
    private NameMap nameMap;
    private ElementManager elementManager;
    private CalculManager calManager;
    private MetroLine lineA;
    private MetroLine lineB;
    private MetroStation stationA;
    private MetroStation stationB;
    private boolean change;
    private JLabel modelLabel;
    private JComboBox modelBox;
    private JLabel stALabel;
    private JComboBox stALineBox;
    private JComboBox stAStationBox;
    private JLabel stBLabel;
    private JComboBox stBLineBox;
    private JComboBox stBStationBox;
    private JLabel changeLabel;
    private ButtonGroup changeGroup;
    private JRadioButton yesButton;
    private JRadioButton noButton;
    private JButton calButton;
    private JButton closeButton;
    private JPanel display;
    private JTextArea displayArea;
}
